<?php

namespace App\Repository;

use App\Entity\Config;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method Config|null find($id, $lockMode = null, $lockVersion = null)
 * @method Config|null findOneBy(array $criteria, array $orderBy = null)
 * @method Config[]    findAll()
 * @method Config[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ConfigRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Config::class);
    }

    /**
     * @param $k
     * @return string|null
     */
    public function getByK($k)
    {
        $conf = $this->findOneBy(array("k"=>$k));
        if ($conf) {
            return  $conf->getV();
        } else {
            return  "";
        }
    }

    /**
     * @param $k
     * @return string|null
     */
    public function getPicByK($k)
    {
        $conf = $this->findOneBy(array("k"=>$k));
        if ($conf) {
            return  $conf->getPic();
        } else {
            return  "";
        }
    }

    /**
     * 处理成对的数字如1：2,2：5，...这类成对出现的
     * @param $k
     * @return array
     */
    public function getKVNumArray($k)
    {
        $charge = $this->getByK($k);
        $money = myExplodeNumbers($charge);
        $moneySet = [];
        $cound = floor(count($money)/2);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$money[$i * 2],$money[$i * 2 + 1]];
        }
        return $moneySet;
    }
    // /**
    //  * @return Config[] Returns an array of Config objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('c.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Config
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
